]> git.r.bdr.sh - rbdr/super-polarity/blobdiff - Super Polarity/InputController.cs
I have the worst commits ever.
[rbdr/super-polarity] / Super Polarity / InputController.cs
index 405b3abde5fbc3625c359ce835c1ea50071d068d..79bd0391bedb6e0b077365021dc57ce5a7022f09 100644 (file)
@@ -11,6 +11,8 @@ namespace SuperPolarity
         static Dictionary<string, List<Action<float>>> Listeners;
         static Dictionary<string, List<Keys>> RegisteredKeys;
         static Dictionary<string, List<Buttons>> RegisteredButtons;
+        static List<string> BlockedKeys;
+        static List<string> BlockedButtons;
 
         static GamePadState InputGamePadState;
         static KeyboardState InputKeyboardState;
@@ -25,6 +27,10 @@ namespace SuperPolarity
         static InputController()
         {
             Listeners = new Dictionary<string,List<Action<float>>>();
+            RegisteredButtons = new Dictionary<string, List<Buttons>>();
+            RegisteredKeys = new Dictionary<string, List<Keys>>();
+            BlockedKeys = new List<string>();
+            BlockedButtons = new List<string>();
             InputKeyboardState = new KeyboardState();
             InputGamePadState = new GamePadState();
         }
@@ -42,8 +48,82 @@ namespace SuperPolarity
             InputKeyboardState = Keyboard.GetState();
         }
 
+        public static void RegisterEventForKey(string eventName, Keys key)
+        {
+            List<Keys> newKeyList;
+            if (!RegisteredKeys.ContainsKey(eventName))
+            {
+                newKeyList = new List<Keys>();
+                RegisteredKeys.Add(eventName, newKeyList);
+            }
+
+            RegisteredKeys.TryGetValue(eventName, out newKeyList);
+
+            newKeyList.Add(key);
+        }
+
+        public static void RegisterEventForButton(string eventName, Buttons button)
+        {
+            List<Buttons> newButtonList;
+            if (!RegisteredButtons.ContainsKey(eventName))
+            {
+                newButtonList = new List<Buttons>();
+                RegisteredButtons.Add(eventName, newButtonList);
+            }
+
+            RegisteredButtons.TryGetValue(eventName, out newButtonList);
+
+            newButtonList.Add(button);
+        }
+
         private static void DispatchRegisteredEvents()
         {
+            var keyFired = false;
+
+            foreach (KeyValuePair<string,List<Keys>> entry in RegisteredKeys) {
+                keyFired = false;
+                foreach (Keys key in entry.Value)
+                {
+                    if (InputKeyboardState.IsKeyDown(key))
+                    {
+                        if (!BlockedKeys.Contains(entry.Key))
+                        {
+                            BlockedKeys.Add(entry.Key);
+                            Dispatch(entry.Key, 1);
+                        }
+                        keyFired = true;
+                        break;
+                    }
+                }
+
+                if (!keyFired)
+                {
+                    BlockedKeys.Remove(entry.Key);
+                }
+            }
+
+            foreach (KeyValuePair<string, List<Buttons>> entry in RegisteredButtons)
+            {
+                keyFired = false;
+                foreach (Buttons button in entry.Value)
+                {
+                    if (InputGamePadState.IsButtonDown(button))
+                    {
+                        if (!BlockedButtons.Contains(entry.Key))
+                        {
+                            BlockedButtons.Add(entry.Key);
+                            Dispatch(entry.Key, 1);
+                        }
+                        keyFired = true;
+                        break;
+                    };
+                }
+
+                if (!keyFired)
+                {
+                    BlockedButtons.Remove(entry.Key);
+                }
+            }
         }
 
         private static void DispatchMoveEvents()
@@ -55,8 +135,6 @@ namespace SuperPolarity
             xMovement = InputGamePadState.ThumbSticks.Left.X;
             yMovement = -InputGamePadState.ThumbSticks.Left.Y;
 
-            Console.WriteLine("Dispatching Input {0}", InputKeyboardState.IsKeyDown(Keys.Left));
-
             if (InputKeyboardState.IsKeyDown(Keys.Left))
             {
                 xMovement = -1.0f;
@@ -114,5 +192,11 @@ namespace SuperPolarity
                 method(value);
             }
         }
+
+        public static void Unlock(string eventName)
+        {
+            BlockedButtons.Remove(eventName);
+            BlockedKeys.Remove(eventName);
+        }
     }
 }